package com.six.exception;

import com.six.result.MessageCode;
import com.six.result.Result;
import com.six.result.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    //指定出现什么异常执行该方法
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        log.error(e.getMessage());
        return ResultUtils.buildFail(MessageCode.UN_KNOW_EXCEPTION);
    }


    //自定义异常
    @ExceptionHandler(CustomerException.class)
    @ResponseBody
    public Result  customer(CustomerException customerException){
        String msg=customerException.getMessageCode().getMsg();
        Integer code=customerException.getMessageCode().getCode();
        return ResultUtils.buildFail(code,msg);
    }

}
